export default {
    sleep(haomiao) {
        return new Promise((resolve, reject) => {
            setTimeout(function () {
                resolve()
            }, haomiao)
        })
    },
    getSizePackageByName(name) {
        let div = window.document.createElement('div')
        div.className = `c-round-${name} c-width-${name} c-font-size-${name} c-p-${name} c-position-fixed`
        window.document.body.appendChild(div)
        let csd = window.getComputedStyle(div)
        let sizePackage = {
            size: csd.width.replace('px', '') * 1,
            fontSize: csd.fontSize.replace('px', '') * 1,
            borderRadius: csd.borderTopLeftRadius.replace('px', '') * 1,
            space: csd.padding.replace('px', '') * 1,
        }
        div.remove()
        return sizePackage
    },
    getEl(target) {
        if (!target) {
            return target
        }
        if (target instanceof Array) {
            return this.getEl(target[0])
        }
        if (target.$el) {
            return this.getEl(target.$el)
        }
        if ([3, 8].includes(target.nodeType)) {
            return this.getEl(target.nextElementSibling)
        }
        return target
    },

    getBase64FromDataUrl(dataUrl) {
        let base64Sign = 'base64,'
        return dataUrl.substr(dataUrl.indexOf(base64Sign) + base64Sign.length)
    },

    //从input[type=file]获取dataUrl，注意，不是base64编码字符串
    getDataUrlFromFile(file) {
        return new Promise(resolve => {
            let fr = new FileReader()
            fr.onload = function () {
                resolve(fr.result)
            }
            fr.readAsDataURL(file)
        })
    },

    getStorageSizeStr(size) {
        if (!size) {
            return '0B'
        }
        if (size / 1024 < 1) {
            return `${size}B`
        } else if (size / 1024 / 1024 < 1) {
            return `${(size / 1024).toFixed(2)}KB`
        } else if (size / 1024 / 1024 / 1024 < 1) {
            return `${(size / 1024 / 1024).toFixed(2)}MB`
        } else if (size / 1024 / 1024 / 1024 / 1024 < 1) {
            return `${(size / 1024 / 1024 / 1024).toFixed(2)}GB`
        }
        return `${size}B`
    }
}